//
//  LHVistorView.m
//  OCSinaWeibo
//
//  Created by LiHong on 15/10/27.
//  Copyright © 2015年 LiHong. All rights reserved.
//

#import "LHVistorView.h"
#import "Masonry.h"

@interface LHVistorView ()

@property (nonatomic,strong)UIImageView *iconView;///<中心图片
@property (nonatomic,strong)UIImageView *houseView;///<小房子
@property (nonatomic,strong)UILabel *textLabel;///<描述文字
@property (nonatomic,strong)UIButton *loginButton;///<登录按钮
@property (nonatomic,strong)UIButton *registerButton;///<注册按钮
@property (nonatomic,strong)UIImageView *coverView;///<遮盖层
@end

@implementation LHVistorView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self prepareUI];
    }
    return self;
}
#pragma mark - 监听点击事件
/// 登录按钮
- (void)vistorViewWillLogin{
    if ([self.delegate respondsToSelector:@selector(vistorViewWillLogin)]) {
        [self.delegate vistorViewWillLogin];
    }
}
/// 注册按钮
- (void)vistorViewWillRegister{
    if ([self.delegate respondsToSelector:@selector(vistorViewWillRegister)]) {
        [self.delegate vistorViewWillRegister];
    }
}
#pragma mark - 供外部调用方法
///开始旋转
- (void)startRotation{
    CABasicAnimation *animation = [[CABasicAnimation alloc]init];
    animation.keyPath = @"transform.rotation";
    animation.toValue = @(2 * M_PI);
 //   [animation setToValue:@(2 * M_PI)];
    animation.duration = 20;
    animation.repeatCount = MAXFLOAT;
    animation.removedOnCompletion = NO;
    [self.iconView.layer addAnimation:animation forKey:@"iconRotate"];
}
/**
 *  外部设置图片和信息
 *
 *  @param msg     信息
 *  @param imgName 图片
 */
- (void)setupInfoWithMessage:(NSString *)msg imageName:(NSString *)imgName{
    self.houseView.hidden = YES;
    self.iconView.image = [UIImage imageNamed:imgName];
    self.textLabel.text = msg;
    [self sendSubviewToBack:self.coverView];
}
#pragma mark - 布局UI
- (void)prepareUI{
    // 设置背景色
    self.backgroundColor = [UIColor colorWithWhite:237/255.0 alpha:1];
    // 添加子控件
    [self addSubview:self.iconView];
    [self addSubview:self.coverView];
    [self addSubview:self.houseView];
    [self addSubview:self.textLabel];
    [self addSubview:self.loginButton];
    [self addSubview:self.registerButton];
    
    //MARK - 约束
    // 圈圈
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY).with.offset(-40);
        make.centerX.mas_equalTo(self);
    }];
    // 小房子
    [self.houseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.mas_equalTo(self.iconView);
    }];
    // 描述文字
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.equalTo(self.iconView.mas_bottom).with.offset(16);
        make.width.mas_equalTo(@240);
    }];
    // 登录按钮
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.textLabel);
        make.top.equalTo(self.textLabel.mas_bottom).with.offset(16);
        make.width.mas_equalTo(@100);
    //    make.width.equalTo(@100);
        make.height.mas_equalTo(@35);
    }];
    // 注册按钮
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.textLabel);
        make.top.equalTo(self.textLabel.mas_bottom).with.offset(16);
        make.width.mas_equalTo(@100);
        make.height.mas_equalTo(@35);
    }];
    // 遮盖层
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self.registerButton);
    }];
}

#pragma mark - 控件懒加载
- (UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
        [_iconView sizeToFit];
    }
    return _iconView;
}
- (UIImageView *)houseView{
    if (!_houseView) {
        _houseView = [[UIImageView alloc]init];
        _houseView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
        [_houseView sizeToFit];
    }
    return _houseView;
}
- (UILabel *)textLabel{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc]init];
        _textLabel.text = @"关注一些人,看看有什么惊喜";
        _textLabel.numberOfLines = 0;
        _textLabel.textColor = [UIColor lightGrayColor];
    }
    return _textLabel;
}
- (UIButton *)loginButton{
    if (!_loginButton) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_loginButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(vistorViewWillLogin) forControlEvents:UIControlEventTouchUpInside];
        [_loginButton sizeToFit];
    }
    return _loginButton;
}
- (UIButton *)registerButton{
    if (!_registerButton) {
        _registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [_registerButton setBackgroundImage:[UIImage imageNamed:@"common_button_white_disable"] forState:UIControlStateNormal];
        [_registerButton addTarget:self action:@selector(vistorViewWillRegister) forControlEvents:UIControlEventTouchUpInside];
        [_registerButton sizeToFit];
    }
    return _registerButton;
}
- (UIImageView *)coverView{
    if (!_coverView) {
        _coverView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    return _coverView;
}
@end
